<?php
class Tourism_order_product_model extends BZ_Model
{
    protected $table   = 'tourism_order_product';
    protected $table_1 = 'tourism_order';
    protected $table_2 = 'tourism_order_product_profit';
    protected $table_3 = 'tourism_order_refund';

    public function insertTourismOrderProduct($postData, $orderId)
    {
        $data = array(
            'order_id'        => $orderId,
            'goods_attr_id'   => $postData->goods_attr_id,
            'goods_name'      => $postData->goods_name,
            'attribute_value' => !empty($postData->attribute_value) ? $postData->attribute_value : '',
            'goods_image'     => strpos($postData->images, ',') !== false ? strstr($postData->images, ',', true) : $postData->images,
            'number'          => $postData->goods_num,
            'refund_num'      => $postData->goods_num,
            'market_price'    => $postData->market_price,
            'shop_price'      => $postData->shop_price,
            'supply_price'    => $postData->supply_price,
            'pay_amount'      => $postData->pay_amount,
            'swim'            => $postData->swim, //u币的支付
            'calculate_method'=> $postData->calculate_method,
            'rate_id'         => $postData->rate_id,
            'updated_at'      => '0000-00-00 00:00:00',
            'created_at'      => date('Y-m-d H:i:s')
        );
        $this->db->insert($this->table, $data);
        return $this->db->insert_id();
    }

    public  function  findStock($orderId)
    {
        $this->db->select('goods_attr_id,refund_num');
        $this->db->from($this->table);
        $this->db->where('order_id', $orderId);
        return $this->db->get();
    }

    public function addOrderProduct($params)
    {
        $this->db->insert($this->table,$params);
        return $this->db->insert_id();
    }

    public function findByOrderSn($order_sn)
    {
        $this->db->select('goods_attr_id,goods_name');
        $this->db->from($this->table);
        $this->db->where($order_sn);

        return $this->db->get();
    }

    public function findByOrderId($orderId)
    {
        $this->db->select('*');
        $this->db->from($this->table);
        $this->db->where('order_id', $orderId);
        return $this->db->get();
    }

    public function findById($id)
    {
        $this->db->select('tourism_order_product.*,tourism_order.seller_uid');
        $this->db->from($this->table.' AS tourism_order_product');
        $this->db->join($this->table_1.' AS tourism_order','tourism_order_product.order_id = tourism_order.order_id', 'inner');
        $this->db->where('tourism_order_product.order_product_id', $id);
        $this->db->limit(1);
        return $this->db->get();
    }

    public function findOrderProductProfit($orderId)
    {
        $this->db->select('tourism_order_product_profit.order_product_id,tourism_order_product_profit.uid,account,account_type,as,bit_trade');
        $this->db->from($this->table . ' AS tourism_order_product');
        $this->db->join($this->table_2 . ' AS tourism_order_product_profit', 'tourism_order_product.order_product_id=tourism_order_product_profit.order_product_id', 'inner');
        $this->db->where('tourism_order_product.order_id', $orderId);
        return $this->db->get();
    }

    public function findRefundOrder($orderId)
    {
        $this->db->select('attribute_value,tourism_order_product.order_product_id,tourism_order_product.goods_attr_id,pay_amount,tourism_order_product.refund_num,goods_image,goods_name,tourism_order_refund.status,tourism_order_refund.refund_id');
        $this->db->from($this->table . ' AS tourism_order_product');
        $this->db->join($this->table_3 . ' AS tourism_order_refund', 'tourism_order_product.order_product_id=tourism_order_refund.order_product_id', 'left');
        $this->db->where('tourism_order_product.order_id', $orderId);
        //$this->db->group_by('tourism_order_product.order_product_id');
        return $this->db->get();
    }

    public function updateProduct($data)
    {
        $this->db->where('order_product_id',$data['order_product_id']);
        return $this->db->update($this->table, $data);
    }
}